﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Jerry.DroidBuilder.Common;

namespace Jerry.DroidBuilder.ThemeEditor
{
    public partial class LayoutInfo : NotifyPropertyChangedObject
    {
        public LayoutInfo(string layoutType)
        {
            LayoutType = layoutType;
            Values = new Dictionary<string, string>();
            Children = new ObservableCollection<LayoutInfo>();

            SetValue("layout_width", "wrap_content");
            SetValue("layout_height", "wrap_content");

            SetValue("gravity", "center");
            SetValue("layout_gravity", "top|left");
        }

        [Browsable(false)]
        public string Id { get { return GetValue("id"); } }

        [Browsable(false)]
        public string LayoutType { get; private set; }

        [Browsable(false)]
        public Dictionary<string, string> Values { get; private set; }

        [Browsable(false)]
        public ObservableCollection<LayoutInfo> Children { get; private set; }

        #region Methods

        public void SetValue(string name, string value)
        {
            if (!Values.ContainsKey(name))
            {
                Values.Add(name, value);
            }
            else
            {
                Values[name] = value;
            }
        }

        public string GetValue(string name)
        {
            var value = string.Empty;

            Values.TryGetValue(name, out value);

            return value;
        }

        public bool GetBoolean(string name, bool defaultValue)
        {
            var val = GetValue(name);
            var result = defaultValue;

            if (!string.IsNullOrEmpty(val))
            {
                bool.TryParse(val, out result);
            }

            return result;
        }

        public override string ToString()
        {
            return LayoutType;
        }

        #endregion
    }
}
